package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/application")
public class ApplicationController {
    @Autowired
    private ApplicationService applicationService;

    /**
     * 新增保存申请单
     *
     * @param applicationSaveParam
     * @return
     */
    @PostMapping("save")
    public JsonResult saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单,applicationSaveParam={}", applicationSaveParam);
        applicationService.save(applicationSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectApplication(ApplicationQuery applicationQuery) {
        log.debug("applicationQuery={}", applicationQuery);
        List<ApplicationVO> list = applicationService.selectApplication(applicationQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("cancel/{id}")
    public JsonResult cancel(@PathVariable Long id) {
        log.debug("撤销申请单：id{},id");
        applicationService.cancle(id);
        return JsonResult.ok();
    }

    /**
     * 分配车辆
     *
     * @param applicationId
     * @param vehicleId
     * @return
     */
    @PostMapping("distribute/{applicationId}/{vehicleId}")
    public JsonResult distribute(@PathVariable Long applicationId, @PathVariable Long vehicleId) {
        log.debug("分配车辆：applicationId={},vehicleId={}", applicationId, vehicleId);
        applicationService.distribute(applicationId, vehicleId);
        return JsonResult.ok();
    }

    /**
     * 还车
     * @param applicationId
     * @param vehicleId
     * @return
     */
    @PostMapping("back/{applicationId}/{vehicleId}")
    public JsonResult back(@PathVariable Long applicationId, @PathVariable Long vehicleId) {
        log.debug("还车：id={},vehicleId={}", applicationId, vehicleId);
        applicationService.back(applicationId, vehicleId);
        return JsonResult.ok();
    }

}
